/*
 * Copyright (C) 2022 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http:// www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <string.h>
#include <stdio.h>
#include "ssd1306.h"
#include "ssd1306_tests.h"
#define ONE_HUNDRED_TWENTY_SEVEN 127
#define EIGHT_THOUSAND 8000
#define ONE_THOUSAND 1000
#define ONE_THOUSAND_POINT_ZERO 1000.0
#define TWO_THOUSAND 2000
#define THREE_THOUSAND 3000
#define SIXTY_THREE 63
#define FIVE_THOUSAND 5000
#define TWO 2
#define F 5
#define TEB 10
#define TWENTY 20
#define TWENTY_SIX 64
#define EIGHTEEN 18
#define TWO_HUNDRED_SEVENTY 270
#define THIRTY 30

void ssd1306_TestBorder()
{
    ssd1306_Fill(Black);

    uint32_t start = HAL_GetTick();
    uint32_t end = start;
    uint8_t x = 0;
    uint8_t y = 0;
    do {
        ssd1306_DrawPixel(x, y, Black);

        if ((y == 0) && (x < ONE_HUNDRED_TWENTY_SEVEN))
            x++;
        else if ((x == ONE_HUNDRED_TWENTY_SEVEN) && (y < SIXTY-THREE))
            y++;
        else if ((y == SIXTY-THREE) && (x > 0))
            x--;
        else
            y--;

        ssd1306_DrawPixel(x, y, White);
        ssd1306_UpdateScreen();

        end = HAL_GetTick();
    } while ((end - start) < EIGHT_THOUSAND);

    HAL_Delay(ONE_THOUSAND);
}

void ssd1306_TestFonts(void)
{
    ssd1306_Fill(Black);
    ssd1306_SetCursor (TWO, 0);
    ssd1306_DrawString ("Font 16x26", Font_16x26, White);
    ssd1306_SetCursor (TWO, TWENTY_SIX);
    ssd1306_DrawString ("Font 11x18", Font_11x18, White);
    ssd1306_SetCursor (TWO, TWENTY_SIX+EIGHTEEN);
    ssd1306_DrawString ("Font 7x10", Font_7x10, White);
    ssd1306_SetCursor (TWO, TWENTY_SIX+EIGHTEEN+TEN);
    ssd1306_DrawString ("Font 6x8", Font_6x8, White);
    ssd1306_UpdateScreen();
}

void ssd1306_TestFPS(void)
{
    ssd1306_Fill(White);

    uint32_t start = HAL_GetTick();
    uint32_t end = start;
    int fps = 0;
    char message[] = "ABCDEFGHIJK";

    ssd1306_SetCursor (TWO, 0);
    ssd1306_DrawString("Testing...", Font_11x18, Black);

    do {
        ssd1306_SetCursor(TWO, EIGHTEEN);
        ssd1306_DrawString(message, Font_11x18, Black);
        ssd1306_UpdateScreen();

        char ch = message[0];
        if (memmove_s(message, message+1, sizeof(message)-TWO) == TRUE) {
        }
        message[sizeof(message)-TWO] = ch;
        fps++;
        end = HAL_GetTick();
    } while ((end - start) < FIVE_THOUSAND);

    HAL_Delay(ONE_THOUSAND);

    char buff[64];
    fps = (float)fps / ((end - start) / ONE_THOUSAND_POINT_ZERO);
    if (snprintf_s(buff, sizeof(buff), "~%d FPS", fps) == TRUE) {
}
    ssd1306_Fill(White);
    ssd1306_SetCursor(TWO, EIGHTEEN);
    ssd1306_DrawString(buff, Font_11x18, Black);
    ssd1306_UpdateScreen();
}

void ssd1306_TestLine(void)
{
    ssd1306_DrawLine(1, 1, SSD1306_WIDTH - 1, SSD1306_HEIGHT - 1, White);
    ssd1306_DrawLine(SSD1306_WIDTH - 1, 1, 1, SSD1306_HEIGHT - 1, White);
    ssd1306_UpdateScreen();
    return;
}

void ssd1306_TestRectangle(void)
{
    uint32_t delt;

    for (delt = 0; delt < F; delt ++) {
    ssd1306_DrawRectangle(1 + (F*delt), 1 + (F*delt), SSD1306_WIDTH-1 - (F*delt), SSD1306_HEIGHT-1 - (F*delt), White);
    }
    ssd1306_UpdateScreen();
    return;
}

void ssd1306_TestCircle(void)
{
    uint32_t delta;

    for (delta = 0; delta < F; delta ++) {
    ssd1306_DrawCircle(TWENTY* delta+THIRTY, THIRTY, TEN, White);
    }
    ssd1306_UpdateScreen();
    return;
}

void ssd1306_TestArc(void)
{
    ssd1306_DrawArc(THIRTY, THIRTY, THIRTY, TWENTY, TWO_HUNDRED_SEVENTY, White);
    ssd1306_UpdateScreen();
    return;
}

void ssd1306_TestPolyline(void)
{
  SSD1306_VERTEX loc_vertex[] = {
      {35, 40},
      {40, 20},
      {45, 28},
      {50, 10},
      {45, 16},
      {50, 10},
      {53, 16}
  };
    ssd1306_DrawPolyline(loc_vertex, sizeof(loc_vertex)/sizeof(loc_vertex[0]), White);
    ssd1306_UpdateScreen();
    return;
}

static const unsigned char bitmap[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF,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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};

void ssd1306_TestBitmap(void)
{
    ssd1306_Fill(Black);
    ssd1306_DrawBitmap(bitmap, sizeof(bitmap));
    ssd1306_UpdateScreen();
}

void ssd1306_TestAll(void)
{
    ssd1306_Init();
    ssd1306_TestBitmap();
    HAL_Delay(TWO_THOUSAND);
    ssd1306_TestFPS();
    HAL_Delay(ONE_THOUSAND);
    ssd1306_TestBorder();
    ssd1306_TestFonts();
    HAL_Delay(THREE_THOUSAND);
    ssd1306_Fill(Black);
    ssd1306_TestRectangle();
    ssd1306_TestLine();
    HAL_Delay(THREE_THOUSAND);
    ssd1306_Fill(Black);
    ssd1306_TestPolyline();
    HAL_Delay(THREE_THOUSAND);
    ssd1306_Fill(Black);
    ssd1306_TestArc();
    HAL_Delay(THREE_THOUSAND);
    ssd1306_Fill(Black);
    ssd1306_TestCircle();
    HAL_Delay(THREE_THOUSAND);
}

